package com.iqiyi.pps.epg.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public final class DateTimeUtil {
    public static final long DAY_TIME = 86400000L;
    public static final long HOUR_TIME = 3600000L;
    public static final long MINUTE_TIME = 60000L;
    public static final long SECOND = 1000L;
    public static final String SHORT_DATE_TIME = "yyyy-MM-dd";
    public static final String DATE_FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
    private static final String date_format = "yyyy-MM-dd HH:mm:ss";
    private static final String time_format = "HH:mm:ss";
    private static final String SHORT_STR_FORMAT = "yyyyMMdd";
    private static Logger logger = LoggerFactory.getLogger(DateTimeUtil.class);

    private DateTimeUtil() {
    }

    private static SimpleDateFormat getFormat(String format) {
        return new SimpleDateFormat(format);
    }

    private static SimpleDateFormat getLFormat() {
        return new SimpleDateFormat(DATE_FORMAT_LONG);
    }

    private static SimpleDateFormat getSFormat() {
        return new SimpleDateFormat(SHORT_DATE_TIME);
    }

    private static SimpleDateFormat getStrFormat() {return new SimpleDateFormat(SHORT_STR_FORMAT); }

    public static String transShortFormat(long time) {
        return getSFormat().format(new Date(time));
    }

    public static String transLongFormat(long time) {
        return getLFormat().format(new Date(time));
    }

    public static String transStrFormat(long time) {
        return getStrFormat().format(new Date(time));
    }

    public static String transOnlyTimeFormat(long time) {
        return new SimpleDateFormat(time_format).format(new Date(time));
    }
    public static long parseToLong(String dateStr ){
        if( dateStr == null || ( dateStr.length() != 10 && dateStr.length() != 19)  ){
            return 0;
        }
        if( dateStr.length() == 10 ){
            return parseShortFormat( dateStr );
        }else{
            return parseLongFormat( dateStr );
        }
    }

    public static long parseLongFormat(String dateStr) {
        try {
            return getLFormat().parse(dateStr).getTime();
        } catch (ParseException e) {
            logger.error("[parseLongFormat][ParseException]", e);
        }
        return 0;
    }

    public static long parseShortFormat(String dateStr) {
        try {
            return getSFormat().parse(dateStr).getTime();
        } catch (ParseException e) {
            logger.error("[parseShortFormat][ParseException]", e);
        }
        return 0;
    }

    public static  String  getCurrentDay(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date now = new java.util.Date();
        String today = sdf.format(now);
        return today;
    }

    public static String getNextDay(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DAY_OF_YEAR,1);
        String yesterday = sdf.format(ca.getTime());
        return yesterday;
    }

    /**
     * 获取两个时间的相差天数
     *
     * @param dateStr1 ("yyyy-MM-dd")
     * @param dateStr2 ("yyyy-MM-dd")
     * @return 相差天数
     */
    public static int getBetweenDays(String dateStr1, String dateStr2){
        long time1 = parseShortFormat(dateStr1);
        long time2 = parseShortFormat(dateStr2);
        long between_days=(time2 - time1 > 0 ? time2 - time1 : time1 - time2)/(DAY_TIME);
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static Date getPreviousDay(String dateStr1){
        SimpleDateFormat sdf = getLFormat();
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(parseShortFormat(dateStr1));
        ca.add(Calendar.DATE, -1);
        return ca.getTime();
      //  return new Date(ca.getTimeInMillis());
    }


    /**
     * 获取当前日期
     * @param delayMinute 延迟大于0，提前小于0(分钟)
     * @return
     */
    public static  String  getCurrentDay(Integer delayMinute){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Long realMill = System.currentTimeMillis()+ delayMinute.longValue()*1000*60;
        java.util.Date now = new java.util.Date(realMill);
        String today = sdf.format(now);
        return today;
    }

    public static long getZeroTime(long startTime){
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(startTime);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTimeInMillis();
    }
    public static long getDayEndTime(long startTime){
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(startTime);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTimeInMillis();
    }

    public static long increaseDate(long startTime, int days) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(startTime);
        c.add(Calendar.DATE, days);
        return c.getTimeInMillis();
    }

    public static void main(String[] args) {
        long startTime = parseToLong("2017-06-13 19:10:55" );
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(startTime);
        System.out.println(c.get(Calendar.YEAR));
        System.out.println(c.get(Calendar.MONTH)+1);
        System.out.println(c.get(Calendar.DAY_OF_MONTH));
        System.out.println(c.get(Calendar.HOUR_OF_DAY));
        System.out.println(c.get(Calendar.MINUTE));
        System.out.println(c.get(Calendar.SECOND));

    }
}
